<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: install.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Installer extends Engine_Package_Installer_Module {
	
	function onPreinstall() {
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'album' )->limit ( 1 );
		
		$checkModule = $select->query ()->fetch ();
		
		if (! empty ( $checkModule )) {
			$PRODUCT_TYPE = 'sitealbum';
			$PLUGIN_TITLE = 'Sitealbum';
			$PLUGIN_VERSION = '4.1.7';
			$PLUGIN_CATEGORY = 'plugin';
			$PRODUCT_DESCRIPTION = 'Advanced Photo Albums Plugin';
			$_PRODUCT_FINAL_FILE = 0;
			$SocialEngineAddOns_version = '4.1.8';
			$PRODUCT_TITLE = 'Advanced Photo Albums';
			$file_path = APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/ilicense.php";
			$is_file = file_exists ( $file_path );
			if (empty ( $is_file )) {
				include_once APPLICATION_PATH . "/application/modules/Sitealbum/controllers/license/license3.php";
			} else {
				include_once $file_path;
			}
			parent::onPreinstall ();
		} else {
			$base_url = Zend_Controller_Front::getInstance ()->getBaseUrl ();
			return $this->_error ( "<span style='color:red'>Note: You have not installed the Album Plugin on your site. Please first install Album Plugin on your site and then install this Plugin.</span><br/> <a href='" . $base_url . "/manage'>Click here</a> to go to Manage Packages." );
		}
	}
	
	function onInstall() {
		$db = $this->getDb ();
		$table_exist = $db->query ( 'SHOW TABLES LIKE \'engine4_album_photos\'' )->fetch ();
		if (! empty ( $table_exist )) {
			$column_exist = $db->query ( 'SHOW COLUMNS FROM engine4_album_photos LIKE \'photo_hide\'' )->fetch ();
			if (empty ( $column_exist )) {
				$db->query ( 'ALTER TABLE `engine4_album_photos` ADD `photo_hide` BOOL NOT NULL DEFAULT "0";' );
			}
		}
		
		$db->query ( "UPDATE  `engine4_socialengineaddons` SET  `is_activate` =  '1' WHERE  `engine4_socialengineaddons`.`module_name` ='sitealbum';" );
		
		parent::onInstall ();
	}
	
	function onEnable() {
		$this->updateAlubmsConfiguration ( "0" );
		parent::onEnable ();
	}
	
	function onDisable() {
		$this->updateAlubmsConfiguration ( "1" );
		parent::onDisable ();
	}
	
	public function updateAlubmsConfiguration($enable) {
		$db = $this->getDb ();
		$db->query ( "UPDATE `engine4_core_menuitems` SET `enabled` = '$enable' WHERE `engine4_core_menuitems`.`name` ='core_main_album' LIMIT 1 ;" );
		$db->query ( "UPDATE `engine4_core_menuitems` SET `enabled` = '$enable' WHERE `engine4_core_menuitems`.`name` ='core_sitemap_album' LIMIT 1 ;" );
		$db->query ( "UPDATE `engine4_core_menuitems` SET `enabled` = '$enable' WHERE `engine4_core_menuitems`.`name` ='mobi_browse_album' LIMIT 1 ;" );
	}

}
?>
